package org.nnxy.foodboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nnxy.foodboot.entity.UserRating;
import org.nnxy.foodboot.mapper.UserRatingMapper;
import org.nnxy.foodboot.service.PopularRecipeService;
import org.nnxy.foodboot.service.UserRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户评分服务实现类
 */
@Service
public class UserRatingServiceImpl extends ServiceImpl<UserRatingMapper, UserRating> implements UserRatingService {

    @Autowired
    private UserRatingMapper userRatingMapper;
    
    @Autowired
    private PopularRecipeService popularRecipeService;
    
    /**
     * 添加或更新评分
     *
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @param rating 评分
     * @param comment 评论内容
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrUpdateRating(Long userId, Long recipeId, Integer rating, String comment) {
        // 先查询用户是否已评分
        UserRating userRating = getUserRating(userId, recipeId);
        boolean isUpdate = userRating != null;
        
        if (userRating == null) {
            userRating = new UserRating();
            userRating.setUserId(userId);
            userRating.setRecipeId(recipeId);
        }
        
        userRating.setRating(rating);
        userRating.setComment(comment);
        
        boolean result = this.saveOrUpdate(userRating);
        
        // 当添加新评分时，更新热门食谱指标
        if (result && !isUpdate) {
            popularRecipeService.updateRecipePopularityMetrics(recipeId, 0, 1, 0);
        }
        
        return result;
    }
    
    /**
     * 删除评分
     *
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRating(Long userId, Long recipeId) {
        // 查询是否存在
        int count = (int) this.count(
                new LambdaQueryWrapper<UserRating>()
                    .eq(UserRating::getUserId, userId)
                    .eq(UserRating::getRecipeId, recipeId)
                    .eq(UserRating::getDelFlag, "0")
        );
        
        if (count == 0) {
            return false;
        }
        
        // 逻辑删除
        return this.update(
                new LambdaQueryWrapper<UserRating>()
                    .eq(UserRating::getUserId, userId)
                    .eq(UserRating::getRecipeId, recipeId)
                    .eq(UserRating::getDelFlag, "0")
        );
    }
    
    /**
     * 获取食谱平均评分
     *
     * @param recipeId 食谱ID
     * @return 平均评分
     */
    @Override
    public BigDecimal getAverageRating(Long recipeId) {
        return userRatingMapper.getAverageRating(recipeId);
    }
    
    /**
     * 获取用户对食谱的评分
     *
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 用户评分
     */
    @Override
    public UserRating getUserRating(Long userId, Long recipeId) {
        return userRatingMapper.getUserRating(userId, recipeId);
    }
    
    /**
     * 获取食谱的所有评价
     *
     * @param recipeId 食谱ID
     * @return 评价列表
     */
    @Override
    public List<UserRating> getRecipeRatings(Long recipeId) {
        return userRatingMapper.getRecipeRatings(recipeId);
    }
} 